package com.zeronx.controller;

import com.zeronx.entity.User;
import com.zeronx.utils.ControllerMsg;
import com.zeronx.utils.I18nUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author zeronx on 2017/3/28.
 * @version 1.0
 */
@Controller
@RequestMapping(value = "index")
public class TestController extends BaseController {

    @Autowired
    private ControllerMsg controllerMsg;

    public static final String LOCALE_REQUEST_ATTRIBUTE_NAME = CookieLocaleResolver.class.getName() + ".LOCALE";

    @RequestMapping("obj")
    public Object returnObj() {
        // it will be find http://localhost:8080/index/obj.jsp
        return  new Object();
    }

    /**
     * 演示一些消息
     */
    @RequestMapping("something")
    @ResponseBody
    public Object something(HttpServletRequest request){
        // 获取的是浏览器客户端的本地locale
        Locale locale = request.getLocale();
        // 获取基于cookie 的
        Locale locale2 = (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
        I18nUtils i18nUtils = new I18nUtils(locale2);
        i18nUtils.init(controllerMsg);
        System.out.println(controllerMsg.getName());
        System.out.println(controllerMsg.getPasswd());
        System.out.println("coming into something page");
        return "#{something}";
    }

    /**
     * 换页面
     */
    @RequestMapping("welcome")
    public String welcome(){
        System.out.println("coming into welcome page");
        return "welcome";
    }

    @RequestMapping("data")
    @ResponseBody
    public Object getData(){
        System.out.println("coming into data");
        User user = new User();
        user.setId("#{user.id}");
        user.setName("小莉");

        User user1 = new User();
        user1.setId("#{user.id1}");
        user1.setName("#{user.name}");
        User user2 = new User();
        user2.setId("#{user.id6}");
        user2.setName("小莉22");
        User user3 = new User();
        user3.setId("#{user.id3}");
        user3.setName("小莉33");
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);

        user.setList(list);
        user.setUser(user3);

        return user;
    }
}
